import { post } from '@/http/axios'
import { handleData } from '@/util/data'

// description	角色描述
// is_free	是否免费角色
// name	角色名称
// sys_role	是否系统角色
export const addAuthData = (...args) => post('/addRole', ...args)

// token	token
// description	角色描述
// id	角色id
// is_free	是否免费角色
// name	角色名称
// sys_role	是否系统角色
export const editAuthData = (...args) => post('/editRoleById', ...args)

//id
export const delAuthData = (...args) => post('/removeRoleById', ...args)

// id
// permissionIds 功能权限id列表
export const selectAuthFeature = (...args) =>
  post('/editPermissionRole', ...args)

// id
// assetPositionIds 设备位置权限id列表
// assetCategoryIds 设备类型权限id列表
// workTypeRoleIds 工单类型权限id列表
export const selectAuthData = (...args) =>
  post('/editDataPermissionRole', ...args)

// pageNumber	当前页码
// pageSize	每页展示行数
// keywordSearch	关键字
const roleConfig = [
  ['id', 'id', 'name', 'description'],
  ['nid', 'id', 'label', 'remark']
]
export const getRoleData = async (...args) => {
  const { code, data, cancelMessage } = await post('/searchRoleList', ...args)

  if (data) {
    return {
      code,
      data: [
        {
          nid: 'node_id_system',
          label: '默认角色',
          children: handleData(data.sysRoleList, roleConfig)
        },
        {
          nid: 'node_id_custom',
          label: '自定义角色',
          children: handleData(data.noSysRoleList, roleConfig)
        }
      ],
      cancelMessage
    }
  }
  return { code, cancelMessage }
}

// id: '企业id',
// name: '名称',
// key: 'cloud',
// url: '键',
// root_url: null,
// position_url: null,
// asset_url: null,
// parent_id: '父菜单',
// is_use: '是否可用',
// create_time: null,
// index: '顺序',
// icon: '样式名',
// type: '菜单还是功能1:菜单2:功能3:操作',
// typeName: null,
// tree_type: 0,
// isSelect: '是否选中1选中2未选中',
// childrens: null
//特殊处理
const toNormalAuthFeatureData = (data = [], parent) => {
  const arr = []
  if (data) {
    data.forEach((item, i) => {
      const {
        id,
        type,
        parent_id,
        url,
        key,
        icon,
        menu_name: name,
        childrens,
        isSelect
      } = item
      const obj = {
        nid: parent ? parent.nid + i : i + '',
        id,
        parentId: parent_id || '',
        url: url || '',
        icon: icon,
        key,
        name,
        feature: []
      }
      if (type === 1 || type === 2) {
        obj.all = false
        obj.select = []
        arr.push(obj)
        parent && (obj.parent = parent)
      }
      if (type === 3) {
        obj.auth = isSelect === 1 ? true : false
        if (parent) {
          parent.feature.push(obj)
          obj.auth && parent.select.push(obj.id)
        }
      }
      if (
        parent &&
        parent.feature.length &&
        parent.select.length === parent.feature.length
      ) {
        parent.all = true
      }
      obj.children = toNormalAuthFeatureData(childrens, obj)
    })
  }
  return arr
}
// getAuthFeatureData
export const getAuthFeatureData = async (...args) => {
  const { code, data, cancelMessage } = await post(
    '/searchRolePermission',
    ...args
  )
  if (data) {
    return { code, data: toNormalAuthFeatureData(data), cancelMessage }
  }
  return { code, cancelMessage }
}

const handleAuthTree = (data = [], parent) => {
  const arr = []
  data.forEach(({ label, value, children, type }) => {
    const obj = {
      label,
      value,
      auth: type === 1 ? true : false
    }
    parent && (obj.parent = parent)
    children &&
      children.length &&
      (obj.children = handleAuthTree(children, obj))
    arr.push(obj)
  })
  return arr
}

const handleAuthData = data => {
  return { ...handleAuthSelect(data), data }
}
const handleAuthSelect = (data = []) => {
  const obj = {
    all: true,
    count: 0,
    select: []
  }
  data.map(({ value, auth, children }) => {
    auth ? obj.select.push(value) : (obj.all = false)
    obj.count++
    if (children) {
      const { all, count, select } = handleAuthSelect(children)
      obj.select.push(...select)
      obj.all && (obj.all = all)
      obj.count += count
    }
  })
  return obj
}

const positionConfig = [
  ['position_name', 'position_code', 'childrens', 'isSelect'],
  ['label', 'value', 'children', 'type']
]
// id
export const getAuthPositionData = async (...args) => {
  const { code, data, cancelMessage } = await post(
    '/searchRoleAssetPositionPermission',
    ...args
  )
  if (data) {
    return {
      code,
      data: handleAuthData(handleAuthTree(handleData(data, positionConfig))),
      cancelMessage
    }
  }
  return { code, cancelMessage }
}

const eqptTypeConfig = [
  ['category_name', 'id', 'childrens', 'isSelect'],
  ['label', 'value', 'children', 'type']
]
// id
export const getAuthEqptTypeData = async (...args) => {
  const { code, data, cancelMessage } = await post(
    '/searchRoleAssetTypePermission',
    ...args
  )
  if (data) {
    return {
      code,
      data: handleAuthData(handleAuthTree(handleData(data, eqptTypeConfig))),
      cancelMessage
    }
  }
  return { code, cancelMessage }
}

const workTypeConfig = [
  ['type_name', 'id', 'isSelect'],
  ['label', 'value', 'type']
]
// id
export const getAuthWorkTypeData = async (...args) => {
  const { code, data, cancelMessage } = await post(
    '/searchRoleWorkTypePermission',
    ...args
  )
  if (data) {
    return {
      code,
      data: handleAuthData(handleAuthTree(handleData(data, workTypeConfig))),
      cancelMessage
    }
  }
  return { code, cancelMessage }
}

//searchRoleListPermission

export const getAuth = (...args) => post('/searchRoleListPermission', ...args)

//id
export const cloneRole = (...args) => post('/cloneRole', ...args)
